<template>
  <BasicDrawer @register="registerGoodsWechatDrawer" :title="title" showFooter width="40%" @ok="handleOk" destroyOnClose>
    <a-tabs v-model:activeKey="activeKey" @change="handleTabChange" size="small">
      <a-tab-pane tab="上架公众号" key="form1">
        <GoodsWechatSet ref="form1"></GoodsWechatSet>
      </a-tab-pane>
      <a-tab-pane tab="首页推荐" key="form2">
        <GoodsWechatRecommendHome ref="form2"></GoodsWechatRecommendHome>
      </a-tab-pane>
      <a-tab-pane tab="商城推荐" key="form3">
        <GoodsWechatRecommendMall ref="form3"></GoodsWechatRecommendMall>
      </a-tab-pane>
    </a-tabs>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { defineEmits, getCurrentInstance, nextTick, ref } from 'vue';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import GoodsWechatSet from './GoodsWechatSet.vue';
import GoodsWechatRecommendHome from './GoodsWechatRecommendHome.vue';
import GoodsWechatRecommendMall from './GoodsWechatRecommendMall.vue';

const title = ref('');
//当前选中tab
const activeKey = ref('form1');
const emit = defineEmits(['success', 'register']);
const ctx: any = getCurrentInstance();
//当前列表编辑数据对象
const record = ref();
const [registerGoodsWechatDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  record.value = data.record;

  handleTabChange();
});

/**
 * tab切换事件
 */
function handleTabChange() {
  nextTick(() => {
    if (activeKey.value == 'form1') {
      title.value = record.value.goodsName + '-上架公众号设置';
    }
    if (activeKey.value == 'form2') {
      title.value = record.value.goodsName + '-首页推荐设置';
    }
    if (activeKey.value == 'form3') {
      title.value = record.value.goodsName + '-商城推荐设置';
    }
    ctx.refs[activeKey.value].setData(record.value);
  });
}

/**
 * 确定按钮事件
 */
async function handleOk() {
  try {
    setDrawerProps({ confirmLoading: true });
    ctx.refs[activeKey.value].handleSubmit((callback) => {
      //执行子组件回调事件
      callback && callback(emit);
      setDrawerProps({ confirmLoading: false });
      //关闭抽屉
      //closeDrawer();
    });
  } catch (err) {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
